package com.zhu.app.dwd;

import com.zhu.utils.ZhuKafkaUtil;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

/**
 * 用户使用优惠券下单时，优惠券领用表的 using_time 字段会更新为下单时间，同时
 * coupon_status 字段会由 1401 更改为 1402，因此优惠券下单数据应满足三个条件：① 操
 * 作类型为 update；② 当前 coupon_status 字段的值为 1402；③ 修改了 coupon_status 字
 * 段。
 */
public class DWDToolCouponOrderApp {
    public static void main(String[] args) throws Exception {

        //todo env
        StreamExecutionEnvironment streamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment();
        streamExecutionEnvironment.setParallelism(1); //  kafka partition 4

        //check point
           /*
        streamExecutionEnvironment.setStateBackend(new HashMapStateBackend());
        streamExecutionEnvironment.getCheckpointConfig().setCheckpointStorage(ClusterParametersConfig.HDFS_CHECKPOINT_FILE_DIR);  //检查点保存在hdfs
        System.setProperty("HADOOP_USER_NAME", "zhu");
        streamExecutionEnvironment.getCheckpointConfig().setCheckpointTimeout(10 * 60000L);  //TimeOut
        streamExecutionEnvironment.getCheckpointConfig().setMaxConcurrentCheckpoints(2);  //最大共存检查点
        streamExecutionEnvironment.setRestartStrategy(RestartStrategies.fixedDelayRestart(3, 5 * 1000L));  //重启策略
        */
        StreamTableEnvironment streamTableEnvironment = StreamTableEnvironment.create(streamExecutionEnvironment);
        Configuration configuration = streamTableEnvironment.getConfig().getConfiguration();
        configuration.setString("table.exec.state.ttl", "5 s");  //状态存活时间

        //todo kafka topic_db
        streamTableEnvironment.executeSql(ZhuKafkaUtil.getTopicDB("dwd_tool_coupon_order"));

        //todo  couponUserOrder
        Table result_table = streamTableEnvironment.sqlQuery(
                "select " +
                        "data['id'] id, " +
                        "data['coupon_id'] coupon_id, " +
                        "data['user_id'] user_id, " +
                        "data['order_id'] order_id, " +
                        "date_format(data['using_time'],'yyyy-MM-dd') date_id, " +
                        "data['using_time'] using_time " +
                        "from topic_db " +
                        "where `table` = 'coupon_use' "
                        /*
                        "and `type` = 'update' " +
                        "and data['coupon_status'] = '1402' " +
                        "and `old`['coupon_status'] = '1401'"

                         */
        );
        streamTableEnvironment.createTemporaryView("result_table",result_table);

        // TODO 5. 建立 Kafka-Connector dwd_tool_coupon_order 表
        streamTableEnvironment.executeSql("create table dwd_tool_coupon_order(\n"
                +
                "id string,\n" +
                "coupon_id string,\n" +
                "user_id string,\n" +
                "order_id string,\n" +
                "date_id string,\n" +
                "order_time string\n" +
                ")" +
                ZhuKafkaUtil.getKafkaSinkDDL("dwd_tool_coupon_order"));
        // TODO 6. 将数据写入 Kafka-Connector 表
        streamTableEnvironment.executeSql("" +
                "insert into dwd_tool_coupon_order select " +
                "id,\n" +
                "coupon_id,\n" +
                "user_id,\n" +
                "order_id,\n" +
                "date_id,\n" +
                "using_time order_time\n" +
                " from result_table");
    }
}
